package model;

import java.util.Iterator;

public class GraphDirector {
	
	private static GraphDirector instance = null;
	
	private IGraphReader gr = null;
	private IGraphWriter gw = null;
	
	public GraphDirector(){}
	
	public static GraphDirector getInstance(){
		
		if(instance == null) {
	         instance = new GraphDirector();
	      }
		return instance;
	}

	public Graph createGraph() {
		if(gr != null){
			Graph g = new Graph();
			Module m = gr.getModule();
			while(m != null){
				g.addModule(m);
				m = gr.getModule();
			}
			Iterator<String> mNames = g.getModules().keySet().iterator();
			
			String src = null;
			while(mNames.hasNext()){
				src = mNames.next();
				Iterator<String> rs = gr.getRelationships(src).iterator();
				String target = null;
				while(rs.hasNext()){
					target = rs.next();
					g.addRelationship(new Relationship(g.getModule(src),g.getModule(target)));
				}
			}
			
			return g;
		}
		else return null;
	}

	public void saveGraph(Graph aGraph) {
		throw new RuntimeException("Not yet implemented");
	}

	public IGraphReader getGraphReader() {
		return gr;
	}

	public void setGraphReader(IGraphReader gr) {
		this.gr = gr;
	}

	public IGraphWriter getGraphWriter() {
		return gw;
	}

	public void setGraphWriter(IGraphWriter gw) {
		this.gw = gw;
	}
}